/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.FeedBack;
import com.wolfpire.proms.model.FeedBackAudit;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.FeedBackService;
import com.wolfpire.proms.service.TaskPlanService;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.excel2.ExcelCellData;
import com.wolfpire.system.common.excel2.ExcelTable;
import com.wolfpire.system.common.excel2.ExportExcel;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;

/**
 * 任务反馈控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/feedback")
public class FeedBackController {
	
	@Resource private FeedBackService feedBackService;
	
	@Resource private TaskService taskService;
	
	@Resource private TaskPlanService taskPlanService;
	
	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("proms/feedback/feedback_list");
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<FeedBack> queryList(EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, FeedBack feedBack) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_add");
		if(feedBack == null) feedBack = new FeedBack();
		if (null != feedBack.getTaskId()) {
			Task task = this.taskService.getProjectIndexInfoTask(feedBack.getTaskId());
			feedBack.setTask(task);
		}
		if(null != feedBack.getPlanId()){
			TaskPlan taskPlan = this.taskPlanService.get(feedBack.getPlanId());
			feedBack.setPreAmount(taskPlan.getPlanAmount());
			feedBack.setName(taskPlan.getName());
			feedBack.setTaskPlan(taskPlan);
		}
		mv.getModelMap().addAttribute(feedBack);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(@PrincipalArgument User user, FeedBack feedBack) {
		try {
			/*if(null != feedBack.getId()){
				FeedBack _feedBack = this.feedBackService.get(feedBack.getId());
				feedBack.setCreateTime(_feedBack.getCreateTime());
				feedBack.setCreateUserId(_feedBack.getCreateUserId());
				feedBack.setCreateUserName(_feedBack.getCreateUserName());
				feedBack.setPlanId(_feedBack.getPlanId());
				feedBack.setPreAmount(_feedBack.getPreAmount());
			}*/
			feedBack = this.feedBackService.saveOrUpdate(feedBack, user);
			//this.feedBackService.taskCallback(feedBack);
			this.feedBackService.taskPlanCallback(feedBack);
			Result rs = new Result(true, "保存成功");
			rs.setData(feedBack);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_edit");
		if (null != id) {
			FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
			mv.addObject(feedBack);
		}
		return mv;
	}
	
	@RequestMapping("/detail")
	public ModelAndView detail(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_detail");
		try {
			if (null != id) {
				FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
				if (null != feedBack.getPlanId()) {
					// 这里做一次判空处理
					TaskPlan tp = this.taskPlanService.get(feedBack.getPlanId());
					feedBack.setTaskPlan(tp);
				}
				mv.addObject(feedBack);
			}
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id")Long id) {
		try {
			this.feedBackService.del(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@RequestMapping("/audit")
	public ModelAndView audit(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_audit");
		if (null != id) {
			FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
			mv.addObject(feedBack);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/saveAudit")
	public Result saveAudit(@PrincipalArgument User user, FeedBackAudit feedBackAudit) {
		try {
			feedBackAudit.setCreateUserId(user.getId());
			feedBackAudit.setCreateUserName(user.getNickName());
			this.feedBackService.saveAudit(feedBackAudit);
			if(feedBackAudit.getStatus() == com.wolfpire.system.common.Constants.FEEDBACK_AUDIT_AGREE){
				FeedBack feedBack = this.feedBackService.get(feedBackAudit.getFeedbackId());
				this.feedBackService.taskCallback(feedBack);
			}
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/auditList")
	public ModelAndView auditList(@PrincipalArgument User user, @RequestParam Long menuId,@RequestParam(value = "type", required = false) String type) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		if(type!=null){
			if(type.equals("tree")){
				return new ModelAndView("proms/feedback/feedback_auditList_tree", "operAuthorities", operAuthorities);
			}else{
				return new ModelAndView("proms/feedback/feedback_auditList", "operAuthorities", operAuthorities);
			}
		}
		return new ModelAndView("proms/feedback/feedback_auditList", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryAuditList")
	public EasyuiPagination<FeedBack> queryAuditList(EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataAuditList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
	
	@RequestMapping("/myList")
	public ModelAndView myList(@PrincipalArgument User user, @RequestParam Long menuId) {
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/feedback/feedback_myList", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryMyList")
	public EasyuiPagination<FeedBack> queryMyList(@PrincipalArgument User user, EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		filterFeedBack.setCreateUserId(user.getId());
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
	
	@RequestMapping("/taskFeedbackList")
	public ModelAndView taskFeedbackList(@PrincipalArgument User user,@RequestParam(value = "taskId", required = true) Long taskId) {
		ModelAndView mv = new ModelAndView("proms/feedback/task_feedback_list");
		mv.getModelMap().put("taskId", taskId);
		return mv;
	}
	
	@RequestMapping("/auditedList")
	public ModelAndView auditedList(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/feedback/feedback_auditedList", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryAuditedList")
	public EasyuiPagination<Map<String, Object>> queryAuditedList(EasyuiPage<Map<String, Object>>page, FeedBack filterFeedBack) {
		Page<Map<String, Object>> feedBacks = this.feedBackService.setPageDataAuditedList(page, filterFeedBack);
		return new EasyuiPagination<Map<String, Object>>(feedBacks);
	}
	
	@RequestMapping("/exportExcel")
	public void exportExcel(HttpSession session, 
			HttpServletRequest request, 
			HttpServletResponse response,
			FeedBack filterFeedBack) {
		try {
			List<Map<String, Object>> feedBacks = this.feedBackService.findAuditedList(filterFeedBack);
			ExcelTable table = xlsDto2Excel(feedBacks, request);
			ExportExcel.exportExcel(request, response, table);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//任务名称、计划名称、计划使用资金、实际使用资金、反馈人，执行时间
	private ExcelTable xlsDto2Excel(List<Map<String, Object>> lists, HttpServletRequest request) {
		ExcelTable table = new ExcelTable("已审核反馈记录");
		Map<String, Object> xlsDto = new HashMap<String, Object>();
		int CountColumnNum = 6;
		String[] names = new String[CountColumnNum];
		names[0] = "任务名称";
		names[1] = "计划名称";
		names[2] = "计划使用资金";
		names[3] = "实际使用资金";
		names[4] = "反馈人";
		names[5] = "执行时间";
		
		for (int j = 0; j < CountColumnNum; j++) {  
        	table.addData(new ExcelCellData(0,j,names[j]));
        }
		
		int k = 1;
        for (int i = 0; i < lists.size(); i++) {
        	xlsDto = lists.get(i);  
        	if(null!=xlsDto.get("task_name")){
        		table.addData(new ExcelCellData(k, 0, (String) xlsDto.get("task_name")));
        	}
        	if (null != xlsDto.get("task_plan_name")) {
        		table.addData(new ExcelCellData(k, 1, xlsDto.get("task_plan_name").toString()));
			} 
        	if (null != xlsDto.get("pre_amount")) {
        		table.addData(new ExcelCellData(k, 2, xlsDto.get("pre_amount").toString()));
			} else {
				table.addData(new ExcelCellData(k, 2, "0.00"));
			}
        	if (null != xlsDto.get("fact_amount")) {
        		table.addData(new ExcelCellData(k, 3, xlsDto.get("fact_amount").toString()));
			} else {
				table.addData(new ExcelCellData(k, 3, "0.00"));
			}
        	if (null != xlsDto.get("create_user_name")) {
        		table.addData(new ExcelCellData(k, 4, xlsDto.get("create_user_name").toString()));
			} 
        	if (null != xlsDto.get("execute_time")) {
        		table.addData(new ExcelCellData(k, 5, xlsDto.get("execute_time").toString()));
			} 
        	k++;
        }
		return table;
	}
}
